package top.dreamlove.demo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Pool {
    public static void main(String[] args) throws InterruptedException {
        //设置核心线程为2个,总共线程为3个,非核心线程则为1个,设置等待队列为5
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(5));

        Runnable r1 = () -> {
            System.out.println("当前执行线程" + Thread.currentThread().getName());
        };
        //提交单个线程到线程池
        // threadPoolExecutor.execute(r1);

        for (int i = 0; i < 8; i++) {
            threadPoolExecutor.execute(r1);
        }

        //线程池关闭
        //threadPoolExecutor.shutdown();//温和,等待队列任务执行完成后才关闭
        //threadPoolExecutor.shutdownNow();//暴力关闭,如果等待队列还有任务,都抛弃,不执行
        threadPoolExecutor.shutdown();
        //等待一分钟,如果线程池没有关闭则执行里面逻辑
        if(!threadPoolExecutor.awaitTermination(1,TimeUnit.MINUTES)){
            threadPoolExecutor.shutdownNow();
        }
    }
}
